﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Myproject.DataCRM;
using Myproject.DataAccess.factory;

namespace Myproject.DataAccess.sql
{
    /// <summary>
    /// Entity framework platform sql command class
    /// </summary>
    public class EntityFrameworkSql
    {
        /// <summary>
        /// Get generic type list by entity framework sql
        /// </summary>
        /// <typeparam name="T"> Generic type</typeparam>
        /// <param name="SqlString">Entity framework sql string</param>
        /// <returns>generic type list</returns>
        public List<T> RunSqlGetListResultByEntityFrameworkSql<T>(string SqlString)
        {

            List<T> result = new List<T>();
            using (DBFactoryInstance dbinstance = DBFactoryInstance.DbInstance)
            {
                result=dbinstance.ExecuteStoreQuery<T>(SqlString, null).ToList<T>();
            }
            return result;
        }

        /// <summary>
        /// Get generic type list by entity framework sql
        /// </summary>
        /// <typeparam name="T"> Generic type</typeparam>
        /// <param name="SqlString">Entity framework sql string</param>
        /// <param name="paramsvalue">sql script params</param>
        /// <returns>generic type list</returns>
        public List<T> RunSqlGetListResultByEntityFrameworkSql<T>(string SqlString,string[] paramsvalue)
        {

            List<T> result = new List<T>();
            using (DBFactoryInstance dbinstance = DBFactoryInstance.DbInstance)
            {
                if (paramsvalue.Length != 0)
                {
                    result=dbinstance.ExecuteStoreQuery<T>(SqlString, paramsvalue).ToList<T>();
                }
                else
                {
                    result = RunSqlGetListResultByEntityFrameworkSql<T>(SqlString);
                }
            }
            return result;
        }

        public int RunSqlGetResultByEntityFrameworkSql(string sqlstring)
        {
            using (DBFactoryInstance dbinstance = DBFactoryInstance.DbInstance)
            {
                return dbinstance.ExecuteStoreCommand(sqlstring, null);
            }
        }

        public int RunSqlGetResultByEntityFrameworkSql(string sqlstring, string[] paramsvalue)
        {
            using (DBFactoryInstance dbinstance = DBFactoryInstance.DbInstance)
            {
                return dbinstance.ExecuteStoreCommand(sqlstring, paramsvalue);
            }
        }
    }
}
